package com.minhua.jijiehao2.mainframe.circle.companyIdentifyActivity.impl

import android.graphics.Bitmap
import com.baidu.ocr.sdk.OCR
import com.baidu.ocr.sdk.OnResultListener
import com.baidu.ocr.sdk.exception.OCRError
import com.baidu.ocr.sdk.model.IDCardParams
import com.baidu.ocr.sdk.model.IDCardResult
import com.baidu.ocr.sdk.model.OcrRequestParams
import com.baidu.ocr.sdk.model.OcrResponseResult
import com.google.gson.Gson
import com.minhua.jijiehao2.base.BaseMvpPresenter
import com.minhua.jijiehao2.bean.BaseBean
import com.minhua.jijiehao2.bean.LicenseRecognitionBean
import com.minhua.jijiehao2.constants.DefineKey
import com.minhua.jijiehao2.http.CallBack
import com.minhua.jijiehao2.http.ParamsMap
import com.minhua.jijiehao2.mainframe.circle.companyIdentifyActivity.imp.CompanyIdentifyPresenter
import com.minhua.jijiehao2.mainframe.circle.companyIdentifyActivity.imp.CompanyIdentifyView
import com.minhua.jijiehao2.utils.BitmapUtils
import com.minhua.jijiehao2.utils.LoadingDialogUtil
import com.minhua.jijiehao2.utils.SPUtils
import com.minhua.jijiehao2.utils.T
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody


class CompanyIdentifyPresenter : BaseMvpPresenter<CompanyIdentifyView>(), CompanyIdentifyPresenter {

    private val mIdentifyModel = CompanyIdentifyModel()

    override fun uploadImage(bitmap: Bitmap, requestCode: Int) {
        val bitmapToFile = BitmapUtils.bitmapToFile(bitmap)
        val requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), bitmapToFile)
        val part = MultipartBody.Part.createFormData("image", bitmapToFile.name, requestFile)
        mIdentifyModel.uploadImage(part, object : CallBack<String>() {
            override fun onSuccess(t: BaseBean<String>) {
                v?.onUploadImageSuccess(requestCode, t.data ?: "")
            }
        })
    }


    override fun uploadCompanyInfo() {
        val companyName = v?.companyName() ?: ""
        val legalPersonName = v?.legalPersonName() ?: ""
        val iDNumber = v?.iDNumber() ?: ""
        val contactPerson = v?.contactPerson() ?: ""
        val contactTel = v?.contactTel() ?: ""
        val introduction = v?.introduction() ?: ""
        val licensePic = v?.licensePic() ?: ""
        val identityFrontPic = v?.identityFrontPic() ?: ""
        val identityReversePic = v?.identityReversePic() ?: ""
        val holdCardPic = v?.holdCardPic() ?: ""
        //校验数据start
        //校验数据end
        val map = ParamsMap()
        map.put(DefineKey.USER_ID, SPUtils.get(DefineKey.USER_ID, ""))
        map.put("companyName", companyName)
        map.put("legalPersonName", legalPersonName)
        map.put("iDNumber", iDNumber)
        map.put("contactPerson", contactPerson)
        map.put("contactTel", contactTel)
        map.put("introduction", introduction)
        map.put("licensePic", licensePic)
        map.put("identityFrontPic", identityFrontPic)
        map.put("identityReversePic", identityReversePic)
        map.put("holdCardPic", holdCardPic)
        mIdentifyModel.uploadCompanyInfo(map, object : CallBack<String>() {
            override fun onSuccess(t: BaseBean<String>) {
                v?.onUploadInfoSuccess()
            }
        })
    }

    override fun identifyIDCard(bitmap: Bitmap, cardSide: String, requestCode: Int) {
        // 通用文字识别参数设置
        val param = IDCardParams()
        param.isDetectDirection = true
        param.idCardSide = cardSide
        param.imageFile = BitmapUtils.bitmapToFile(bitmap)
        OCR.getInstance().recognizeIDCard(param, object : OnResultListener<IDCardResult> {
            override fun onResult(p0: IDCardResult?) {
                uploadImage(bitmap, requestCode)
            }

            override fun onError(p0: OCRError?) {
                LoadingDialogUtil.dismissDialog()
                T.showShort("身份证${if (cardSide == IDCardParams.ID_CARD_SIDE_FRONT) {
                    "正面"
                } else {
                    "反面"
                }}别失败，请重新上传")
            }
        })
    }

    override fun identifyLicense(bitmap: Bitmap, requestCode: Int) {
        // 营业执照识别参数设置
        val param = OcrRequestParams()
        // 设置image参数
        param.imageFile = BitmapUtils.bitmapToFile(bitmap)
        // 调用营业执照识别服务
        OCR.getInstance().recognizeBusinessLicense(param, object : OnResultListener<OcrResponseResult> {
            override fun onResult(result: OcrResponseResult) {
                val json = Gson().fromJson<LicenseRecognitionBean>(result.jsonRes, LicenseRecognitionBean::class.java)
                val words = json.words_result.单位名称.words
                if (words == "无") {
                    LoadingDialogUtil.dismissDialog()
                    T.showShort("营业执照识别失败，请重新上传")
                    v?.onLicenseSuccess(json)
                } else {
                    uploadImage(bitmap, requestCode)
                }
            }

            override fun onError(error: OCRError) {
                LoadingDialogUtil.dismissDialog()
                T.showShort("营业执照识别失败，请重新上传")
            }
        })
    }
}